package com.tianqu.tms.www.rental.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;

/**
 * 合同查询/修改 - 信息修改录入 controller
 * @author mufeiyu
 */
public class RentalContractSearchAddInputController extends TmsController {
	private Logger logger = Logger.getLogger(RentalContractSearchAddInputController.class);
	RentalContractAddInputViewHelper contractSerch = new RentalContractAddInputViewHelper();

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();	
			
			RentalContractInfo contractInfo = RentalContractInfo.getInstance();
			contractInfo = contractInfo.searchRentalContractDetailInfo(Long.valueOf(uc.getParameter("id")));

			view(contractInfo);
			uc.setAttribute(TmsConstants.VIEWHELP, contractSerch);
			
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 赋值转换
	 * 
	 * @param dto
	 * @return
	 * @throws TmsDaoException 
	 */
	private RentalContractAddInputViewHelper view(RentalContractInfo contractInfo) throws TmsDaoException {
		
		RentalLocationInfo locationInfo = RentalLocationInfo.getInstance();
		locationInfo = locationInfo.searchLocationDetailBySerial(contractInfo.getLocationSerial());
		//ID
		contractSerch.setId(contractInfo.getId());
		//合同编号
		contractSerch.setSerial(contractInfo.getSerial());
		//合同编号保持
		contractSerch.setSerialHold(contractInfo.getSerial());
		//租金
		contractSerch.setRent(StringUtil.getPointBetweenThree(contractInfo.getRent()));	
		//合同开始日期
		contractSerch.setStartDate(DateUtil.getDateByTimeStamp(contractInfo.getStartDate()));
		//合同结束日期
		contractSerch.setEndDate(DateUtil.getDateByTimeStamp(contractInfo.getEndDate()));
		//租位编号
		contractSerch.setLocationSerial(contractInfo.getLocationSerial());
		//租位编号保持
		contractSerch.setLocationSerialHold(contractInfo.getLocationSerial());
		//租位类型
		Codes code = Codes.getInstance();
		String rentalType = code.getItemValueByItemIdAndItemCode(Codes.RENTAL_TYPE, locationInfo.getRentalType());
		contractSerch.setRentalLocationType(rentalType);
		//租位面积(㎡)
		contractSerch.setAcreage(StringUtil.getPointBetweenThree(locationInfo.getAcreage()));
		//车位数量
		contractSerch.setCarPort(StringUtil.getPointBetweenThree(locationInfo.getCarPort()));
		//租位区域
		contractSerch.setRentalArea(locationInfo.getArea());
		contractSerch.setRentalAreaName(code.getItemValueByItemIdAndItemCode(Codes.RENTAL_AREA, locationInfo.getArea()));
		//合同人姓名
		contractSerch.setOwnerName(contractInfo.getOwnerName());
		//身份证号码
		contractSerch.setOwnerIdCardNo(contractInfo.getOwnerIdCardNo());
		//性别
		if(contractInfo.getOwnerSex() == 2){
			contractSerch.setOwnerSex("女");
		} else {
			contractSerch.setOwnerSex("男");
		}		
		//出生日期
		contractSerch.setOwnerBirthday(DateUtil.getDateByTimeStamp(contractInfo.getOwnerBirthday()));
		//联系电话
		contractSerch.setOwnerPhone(contractInfo.getOwnerPhone());
		//地址
		contractSerch.setOwnerAddress(contractInfo.getOwnerAddress());
		//备注
		contractSerch.setRemark(contractInfo.getRemark());
		//资料图片
		contractSerch.setPhoto_1(contractInfo.getPhoto_1());
		contractSerch.setPhoto_2(contractInfo.getPhoto_2());
		contractSerch.setPhoto_3(contractInfo.getPhoto_3());
		contractSerch.setPhoto_4(contractInfo.getPhoto_4());
		contractSerch.setPhoto_5(contractInfo.getPhoto_5());
		contractSerch.setPhoto_6(contractInfo.getPhoto_6());
		contractSerch.setPhoto_7(contractInfo.getPhoto_7());
		contractSerch.setPhoto_8(contractInfo.getPhoto_8());
		return contractSerch;
	}
}
